Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring (navigator.userAgent.indexOf("MSIE")+5))==6;
Prototype.Browser.IE7 = Prototype.Browser.IE && !Prototype.Browser.IE6;
/* http://www.csscompressor.com/ */
var gSite = '';
var gSiteIm = '';
var gError = false;

function activateForm()
{
	$('webForm').getElements().each(function(elem)
	{
		var dv = elem.getAttribute('dflt');
		switch (elem.type)
		{
			case 'textarea':
				elem.value = dv;
				elem.observe('focus', function() {					
					if (!(elem.hasClassName('error')))
					{
						elem.addClassName('pick');
					}					
					if (((elem.getValue() == dv) || (!(elem.present()))) && (!(elem.hasClassName('error'))))
					{
						elem.value = '';
					}									
				});
				elem.observe('blur', function() {					
					if (!(elem.hasClassName('error')))
					{
						if (((elem.value).strip() === '') || (elem.getValue() == dv))
						{
							elem.removeClassName('pick').value = dv;
						}
						else
						{
							elem.removeClassName('pick');
						}
					}					
				});
			break;

			default:
			break;
		}
	});
}
// Run the tool
function runTool()
{
	var f = $('webForm');
	var errorMsg = '';
	var hasError = false;
	var isRequired = false;
	var isEmpty = false;

	// Form validation
	f.getElements().each(function(elem)
	{
		var dv = elem.getAttribute('dflt');
		isRequired = false;
		isEmpty = (elem.present())?false:true;
		if ((dv && (!isEmpty)) && (elem.getValue().strip() == dv))
		{
			isEmpty = true;
		}
		$w(elem.className).each(function(cn)
		{
			switch(cn)
			{
				case 'required':
					isRequired = true;
				break;
			}
		});
		if ((isRequired && isEmpty) || (isRequired && hasError) || (!isRequired && !isEmpty && hasError))
		{
			hasError = true;
			
			if (errorMsg==='')
			{
				alertError('This is a required field.');
			}
			else
			{
				alertError(errorMsg);
			}
			elem.addClassName('error').value = '';
			elem.focus();				
			
			f.observe('click', function() {
				alertError();
				$(this).stopObserving('click');
				elem.removeClassName('error');
				if ((elem.getValue()).strip() === '')
				{
					elem.value = dv;
				}
			});			
			throw $break;
		}
	});
	if (!hasError)
	{		
		$('loader').setStyle({ display:'' });
		
		var url = gSite + 'controller';
		var pars = '?'+f.serialize() + '&r=' + rand();
		var myAjax = new Ajax.Request(url,{
			method:'post',parameters:pars,
			onCreate:function(){
				$('toolresult').setStyle({ display:'none' });
			},
			onSuccess:function(transport){
				var data = (transport.responseText).evalJSON(true);
				toolOnSuccess(data);				
			},
			onFailure:function(){
				$('loader').setStyle({ display:'none' });
				f.enable();
				f.observe('click', function() {
					alertError();
					$(this).stopObserving('click');
				});
			}
		});
	}
}
function toolOnSuccess(data)
{
	$('loader').setStyle({ display:'none' });	
	$('toolresult').update(data.html);
	Effect.Appear('toolresult', { duration: 2 });	
}
// Reset tool
function resetTool()
{
	$('loader').setStyle({ display:'none' });
	$('toolresult').setStyle({ display:'none' });
}
// Return a random string
function rand()
{
	return Math.round(Math.random()*10000000);
}
// Add to bookmark
function bookmarkthis(title, url)
{
	if (window.sidebar)
	{ // Firefox
		window.sidebar.addPanel(title, url, "");
	}
	else if (document.all)
	{ // IE
		window.external.AddFavorite(url, title);
	}
	else if (window.opera && window.print)
	{ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
}
// Alert box on form
function alertError(msg)
{
	// If there is a string, show the alert
	if (typeof(msg) != 'string')
	{
		$('statusmessage').setStyle({ display:'none' });
	}
	// Else hide the alert
	else
	{
		$('statusmessage').update(msg);
		Effect.Appear('statusmessage', { duration: 1.2 });
	}
}
// Perform these actions onload
Event.observe(window, 'load', function() {
	var f = $('webForm');
	if (f)
	{
		f.observe('submit', runTool).observe('reset', resetTool);
		activateForm();
	}
});
